if (typeof define !== 'function') { var define = require('amdefine')(module); }

define(function () {
    return {
        indexOf: function (arr, item) {
            return arr.indexOf(item);
        },

        sum: function (arr) {
            var sum = 0;

            $.each(arr, function (i, x) {
                sum += x;
            });

            return sum;
        },

        remove: function (arr, item) {
            return arr.filter(function (x) {
                return x != item;
            });
        },

        removeWithoutCopy: function (arr, item) {
            
            while(arr.indexOf(item) !== -1){
                arr.splice(arr.indexOf(item), 1);     
            };

            return arr;
        },

        append: function (arr, item) {
            arr.push(item);
            return arr;
        },

        truncate: function (arr) {
            arr.splice(-1);
            return arr;
        },

        concat: function (arr1, arr2) {
            return arr1.concat(arr2);
        },

        insert: function (arr, item, index) {
            arr.splice(index, 0, item);
            return arr;
        },

        count: function (arr, item) {
            var filtered = arr.filter(function (x){
                return x === item;
            });

            return filtered.length;
        },

        duplicates: function (arr) {
            
            var dups = arr.filter(function(x){
               return arr.indexOf(x) !== arr.lastIndexOf(x);
            });

            var unique = [];
            $.each(dups, function(i, x){
                if (unique.indexOf(x) === -1) { 
                    unique.push(x);
                };
            });

            return unique;
        },

        square: function (arr) {
            return arr.map(function(x){return x*x;})
        },

        findAllOccurrences: function (arr, target) {
            var indexes = [];
            $.each(arr, function(i, x){
                if (x === target) { 
                    indexes.push(i);
                };
            });
            return indexes;
        }
    };
});
